package com.zzusedating.androidapp.fragments;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.zzusedating.androidapp.ProfileActivity;
import com.zzusedating.androidapp.R;
import com.zzusedating.androidapp.customlayout.TextViewWithRightText;
import com.zzusedating.androidapp.tools.MyApplication;
import com.zzusedating.androidapp.tools.ZodiacUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Objects;

public class ProfileFragment extends Fragment {
    private TextView hobbyView;
    private TextView loveDecView;
    private TextViewWithRightText sexView;
    private TextViewWithRightText nameView;
    private TextViewWithRightText idView;
    private TextViewWithRightText phoneView;
    private TextViewWithRightText birthView;
    private TextViewWithRightText residentView;
    private TextViewWithRightText eduBgView;
    private TextViewWithRightText heightView;
    private TextViewWithRightText weightView;
    private TextViewWithRightText constellationView;
    private TextViewWithRightText orgTypeView;
    private TextViewWithRightText monthSalaryView;
    private TextViewWithRightText majorView;
    private TextViewWithRightText emailView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile_layout, container, false);

        birthView = view.findViewById(R.id.FragProfile_Birth);
        constellationView = view.findViewById(R.id.FragProfile_Constellation);
        eduBgView = view.findViewById(R.id.FragProfile_EduBg);
        heightView = view.findViewById(R.id.FragProfile_Height);
        idView = view.findViewById(R.id.FragProfile_IDNumber);
        majorView = view.findViewById(R.id.FragProfile_Major);
        monthSalaryView = view.findViewById(R.id.FragProfile_MonthlyIncome);
        orgTypeView = view.findViewById(R.id.FragProfile_OrgType);
        phoneView = view.findViewById(R.id.FragProfile_Phone);
        nameView = view.findViewById(R.id.FragProfile_RealName);
        residentView = view.findViewById(R.id.FragProfile_Residence);
        sexView = view.findViewById(R.id.FragProfile_Sex);
        weightView = view.findViewById(R.id.FragProfile_Weight);
        hobbyView = view.findViewById(R.id.FragProfile_Hobby);
        loveDecView = view.findViewById(R.id.FragProfile_LoveDec);
        emailView = view.findViewById(R.id.FragProfile_Email);

        nameView.setOnClickListener(listener);

        birthView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        JSONObject detail = ((ProfileActivity) Objects.requireNonNull(getActivity())).getDetail();
        if (((ProfileActivity) getActivity()).getDetail() != null) {
            setData(detail);
        }
        return view;
    }

    public void setData(JSONObject jsonObject){
        if (jsonObject == null || MyApplication.getBasicProfile() == null)
            return;
        JSONObject basicProfile = MyApplication.getBasicProfile();
        try {
            sexView.setRightText(basicProfile.getString("gender"));
            nameView.setRightText(basicProfile.getString("name"));
            emailView.setRightText(basicProfile.getString("email"));
            idView.setRightText(basicProfile.getString("idNum"));
            phoneView.setRightText(basicProfile.getString("phoneNum"));
            birthView.setRightText(basicProfile.getString("birthday"));
            constellationView.setRightText(ZodiacUtil.date2Constellation(basicProfile.getString("birthday")));
            residentView.setRightText(jsonObject.getString("domicile"));
            eduBgView.setRightText(jsonObject.getString("education"));
            majorView.setRightText(jsonObject.getString("major"));
            heightView.setRightText(jsonObject.getString("height"));
            weightView.setRightText(jsonObject.getString("weight"));
            orgTypeView.setRightText(jsonObject.getString("organizationType"));
            monthSalaryView.setRightText(jsonObject.getString("monthlySalary"));
            hobbyView.setText(jsonObject.getString("hobby"));
            loveDecView.setText(jsonObject.getString("loveDeclaration"));
            MyApplication.setDetailProfile(jsonObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public JSONObject getData(){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name", nameView.getRightText());
            jsonObject.put("idNum", idView.getRightText());
            jsonObject.put("phoneNum", phoneView.getRightText());
            jsonObject.put("birthday", birthView.getRightText());
            jsonObject.put("domicile", residentView.getRightText());
            jsonObject.put("education", eduBgView.getRightText());
            jsonObject.put("major", majorView.getRightText());
            jsonObject.put("height", heightView.getRightText());
            jsonObject.put("weight", weightView.getRightText());
            jsonObject.put("organizationType", orgTypeView.getRightText());
            jsonObject.put("monthlySalary", monthSalaryView.getRightText());
            jsonObject.put("hobby", hobbyView.getText());
            jsonObject.put("loveDeclaration", loveDecView.getText());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            final EditText editText = new EditText(getContext());
            new AlertDialog.Builder(getContext())
                    .setView(editText)
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ((TextViewWithRightText)v).setRightText(editText.getText().toString());
                        }
                    })
                    .show();
        }
    };
}
